﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Text;

//CID 1007 //2011.08.16 //Begin
namespace InfoJetSoft.Service.Util
{
    class FloatingPointString
    {
        public static string ToString(double value)
        {
            //CID 1007 //2011.08.11 //Begin
            string doubleString = value.ToString("R", NumberFormatInfo.InvariantInfo);
            if (doubleString.IndexOf("E") > 0)
            {
                string floatString = FloatingPointString.ToFloatingPointString(value);
                return floatString;
            }
            else
            {
                return doubleString;
            }
            //CID 1007 //2011.08.11 //End
        }

        //CID 1007 //2011.08.11 //Begin
        private static readonly Regex rxScientific = new Regex(@"^(?<sign>-?)(?<head>\d+)(\.(?<tail>\d*?)0*)?E(?<exponent>[+\-]\d+)$", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.CultureInvariant);
        //CID 1007 //2011.08.11 //End

        //CID 1007 //2011.08.11 //Begin
        private static string ToFloatingPointString(double value)
        {
            NumberFormatInfo formatInfo = NumberFormatInfo.InvariantInfo;
            string result = value.ToString("R", NumberFormatInfo.InvariantInfo);
            Match match = rxScientific.Match(result);
            if (match.Success)
            {
                int exponent = int.Parse(match.Groups["exponent"].Value, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
                StringBuilder builder = new StringBuilder(result.Length + Math.Abs(exponent));
                builder.Append(match.Groups["sign"].Value);
                if (exponent >= 0)
                {
                    builder.Append(match.Groups["head"].Value);
                    string tail = match.Groups["tail"].Value;
                    if (exponent < tail.Length)
                    {
                        builder.Append(tail, 0, exponent);
                        builder.Append(formatInfo.NumberDecimalSeparator);
                        builder.Append(tail, exponent, tail.Length - exponent);
                    }
                    else
                    {
                        builder.Append(tail);
                        builder.Append('0', exponent - tail.Length);
                    }
                }
                else
                {
                    builder.Append('0');
                    builder.Append(formatInfo.NumberDecimalSeparator);
                    builder.Append('0', (-exponent) - 1);
                    builder.Append(match.Groups["head"].Value);
                    builder.Append(match.Groups["tail"].Value);
                }
                result = builder.ToString();
            }
            return result;
        }
        //CID 1007 //2011.08.11 //End
    }
}
//CID 1007 //2011.08.16 //End